﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
using System.Collections;

namespace Department.App_Code
{
    public class PageProperties  //Getting Page properties
    {
        private static string strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["DepartmentConnectionString"].ConnectionString;

        //Gets Dynamic content type by a given page ID
        public static int GetDynCntType(string id)
        {
            SqlConnection conn = new SqlConnection(strConnString);
            int type = 0;

            try
            {
                conn.Open();


                string sqlType = "SELECT Type.ID FROM Type INNER JOIN Pages ON Type.ID = Pages.TypeID WHERE Pages.MenuID=@ID";
                SqlCommand comType = new SqlCommand(sqlType, conn);
                comType.Parameters.AddWithValue("@ID", id);
                type = Convert.ToInt32(comType.ExecuteScalar());

                return type;

            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
            }
        }

        //Gets the Site content
        public static string GetCnt(string id)
        {
            SqlConnection conn = new SqlConnection(strConnString);
            string text;

            try
            {
                conn.Open();


                string sqlText = "SELECT SiteContent From Pages WHERE MenuID = @ID";
                SqlCommand comText = new SqlCommand(sqlText, conn);
                comText.Parameters.AddWithValue("@ID", id);
                text = Convert.ToString(comText.ExecuteScalar());

                return text;

            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
            }
        }
        //Gets the Page Name
        public static string GetPageName(string id)
        {
            SqlConnection conn = new SqlConnection(strConnString);
            string name;

            try
            {
                conn.Open();


                string sqlPageName = "SELECT Name From Pages WHERE MenuID = @ID";
                SqlCommand comPageName = new SqlCommand(sqlPageName, conn);
                comPageName.Parameters.AddWithValue("@ID", id);
                name = Convert.ToString(comPageName.ExecuteScalar());

                return name;

            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
            }
        }
        public static int GetPageID(string id)
        {
            SqlConnection conn = new SqlConnection(strConnString);
            int pageID;

            try
            {
                conn.Open();


                string sqlPageId = "SELECT ID From Pages WHERE MenuID = @ID";
                SqlCommand comPageId = new SqlCommand(sqlPageId, conn);
                comPageId.Parameters.AddWithValue("@ID", id);
                pageID = Convert.ToInt32(comPageId.ExecuteScalar());

                return pageID;

            }
            catch (Exception ex)
            {
                ex.ToString();

                return 0;
            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
            }
        }

        public static int GetMainPageID() //gets the ID of the main page for the site
        {
            int id;
            SqlConnection conn = new SqlConnection(strConnString);
            try
            {
                conn.Open();
                string sql = "SELECT M.ID FROM Menu M INNER JOIN Pages P ON M.ID = P.MenuID WHERE (P.IsMain = 1)";
                SqlCommand cmd = new SqlCommand(sql, conn);
                id = Convert.ToInt32(cmd.ExecuteScalar());
                
                return id;
            }

             finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
            }
        }

        //Calls a Function that checks if the current user has permissions
        public static bool CallFunction(int pageId, string userId)
        {            
            SqlConnection conn = new SqlConnection(strConnString);
            bool result = false;

            try
            {
                conn.Open();
                string pageSelect = "SELECT dbo.uf_check_rights(@UserID, @PageID)";
                SqlCommand pgCmd = new SqlCommand(pageSelect, conn);
                pgCmd.Parameters.AddWithValue("@UserID", Convert.ToString(userId));
                pgCmd.Parameters.AddWithValue("@PageID", pageId);
                result = Convert.ToBoolean(pgCmd.ExecuteScalar());
                
                return result;

            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
            }
        }

    }
}